package com.wolfsea.launcherdemo
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.*
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.core.view.get
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope

/**
 *@desc TopContentLayoutCenter
 *@author:liuliheng
 *@time: 2023/1/30 16:47
**/
class TopContentLayoutCenter @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : ViewGroup(context, attributeSet), CoroutineScope by MainScope() {

    override fun onFinishInflate() {
        super.onFinishInflate()

        val contentDataList = mutableListOf<ContentData>()

        val dataArray = arrayOf(
            "投资人",
            "暴雪",
            "是编程中",
            "可用模型",
            "解析式",
            "实现",
            "相同",
            "功能",
            "但是",
            "情况下",
            "关键字",
            "加快",
            "目前",
            "重大影响",
            "还不够好",
            "不足以",
            "生命科学",
            "领域",
            "科学家",
            "都说",
            "只能",
            "基因组",
            "前景的",
            "使用循环",
            "我们",
            "利用",
            "函数",
            "映射",
            "发挥些许",
            "应用方向",
            "完全可以",
        )

        for (item in dataArray) {
            contentDataList.add(ContentData(R.mipmap.ic_launcher_round, item))
        }

        addContent(contentDataList)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        var itemSpace = 0
        var totalHeight = 0
        for (i in 0.until(childCount)) {
            val child = get(i)
            measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0)
            val childMeasureWidth = child.measuredWidth
            val layoutParams = child.layoutParams as MarginLayoutParams
            if (i == 0) {
                itemSpace = ((measuredWidth - BaseConstant.ITEMS_PER_ROW * childMeasureWidth).toFloat() / (BaseConstant.ITEMS_PER_ROW - 1)).toInt()
            }
            if (i % BaseConstant.ITEMS_PER_ROW != 0) {
                layoutParams.leftMargin = itemSpace
            }
            layoutParams.topMargin = if (i >= BaseConstant.ITEMS_PER_ROW) context.resources.getDimension(R.dimen.dp_20).toInt() else 0
            if (i % BaseConstant.ITEMS_PER_ROW == 0) {
                totalHeight += child.measuredHeight + layoutParams.topMargin
            }
            child.layoutParams = layoutParams
        }

        setMeasuredDimension(
            MeasureSpec.getSize(widthMeasureSpec) - paddingStart - paddingEnd,
            MeasureSpec.makeMeasureSpec(
                totalHeight,
                MeasureSpec.EXACTLY
            ) - paddingTop - paddingBottom
        )
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        var j = 0
        for (i in 0.until(childCount)) {
            val child = get(i)
            val childMeasuredWidth = child.measuredWidth
            val childMeasuredHeight = child.measuredHeight
            val layoutParams = child.layoutParams as MarginLayoutParams
            val topMargin = layoutParams.topMargin
            val leftMargin = layoutParams.leftMargin

            if (i != 0 && i % BaseConstant.ITEMS_PER_ROW == 0) {
                j++
            }

            val baseCalcLRResult = leftMargin * (i % BaseConstant.ITEMS_PER_ROW)
            val cl = baseCalcLRResult + childMeasuredWidth * (i % BaseConstant.ITEMS_PER_ROW)
            val ct = topMargin * j + childMeasuredHeight * j
            val cr = cl + childMeasuredWidth
            val cb = ct + childMeasuredHeight

            child.layout(
                cl,
                ct,
                cr,
                cb
            )
        }
    }

    override fun generateLayoutParams(attrs: AttributeSet?): LayoutParams {
        return MarginLayoutParams(context, attrs)
    }

    private fun addContent(contentDataSet: MutableList<ContentData>) {
        for (contentDataItem in contentDataSet) {
            val layout = LayoutInflater.from(context).inflate(R.layout.layout_content_center, this, false)
            val itemView = layout.findViewById<LinearLayoutCompat>(R.id.ll_content_center)
            itemView.setOnClickListener {
                context.showToast(contentDataItem.contentDesc)
            }
            val contentIv = layout.findViewById<AppCompatImageView>(R.id.iv_content_center)
            contentIv.setBackgroundResource(contentDataItem.drawRes)
            val contentTv = layout.findViewById<AppCompatTextView>(R.id.tv_content_center)
            contentTv.text = contentDataItem.contentDesc

            addView(
                layout,
                MarginLayoutParams(
                    LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT
                )
            )
        }
    }

    companion object {
        const val TAG = "TopContentLayoutCenter"
    }
}